package servlet.hedong;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import pojo.Comscore;
import pojo.Goods;
import service.ComscoreService;
import service.GoodsService;
import service.UserService;
import service.impl.ComscoreServiceImpl;
import service.impl.GoodsServiceImpl;
import service.impl.UserServiceImpl;
import util.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 积分兑换
 * @author 何栋
 * @parameter
 * @return
 **/
@WebServlet(value = "/integral")
public class IntegralExchangeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pointId=req.getParameter("pointId");//商品积分

        String operate=req.getParameter("operate");//操作类型
        int result=0;//修改结果,0为失败
        HttpSession session = req.getSession();
        Object userId = session.getAttribute("session");//用户id
        UserService userService=new UserServiceImpl();
        int integral=userService.queryIntegral(Integer.parseInt(userId.toString()));//用户积分
         if ("reduce".equals(operate)) {
            if (null!=pointId){
                if (integral<Integer.parseInt(pointId)){
                    req.setAttribute("hint","积分不足");
                    req.getRequestDispatcher("/jsp/hd/alertPage.jsp").forward(req,resp);
                }else {

                   userService.reduceIntegral(integral,Integer.parseInt(userId.toString()));
                    req.getRequestDispatcher("/jsp/hd/Integal.jsp").forward(req,resp);

                }
            }
             System.out.println("积分"+pointId);
        }else {


             PageBean pageBean=new PageBean();//未获取分页信息做准备
             ComscoreService service=new ComscoreServiceImpl();
             session = req.getSession();
             userId = session.getAttribute("session");//用户id
             String uId=  userId.toString();//用户ID的String格式

             PrintWriter out = resp.getWriter();

             String page=req.getParameter("page");//当前页
             String limits=req.getParameter("limit");//每页显示数据条数
//        pageBean.setPageIndex(1);
//        pageBean.setPageSize(2);
             pageBean.setPageIndex(Integer.parseInt(page));//设置当前页数
             pageBean.setPageSize(Integer.parseInt(limits));//设置每页显示数据条数
             JSONObject json=new JSONObject();
             json.put("code",0);
             json.put("msg","");
             pageBean = service.list(pageBean);//调用分页方法，分页信息在此设置
             int count=  pageBean.getTotalCount();//获取分页信息的数据总数
             List<Comscore> com = (List<Comscore>) pageBean.getList();
             json.put("count",count);
             json.put("data", JSONArray.fromObject(com));
             userService=new UserServiceImpl();
             int inte=userService.queryIntegral(Integer.parseInt(uId));
             getServletContext().setAttribute("integral",inte);
             out.print(json);//将需要展示的信息发送到前台
         }

//        int uId=0;
//        if (null!=session.getAttribute("ID").toString()){
//            uId=Integer.parseInt(session.getAttribute("ID").toString());
//        }
//        UserService service=new UserServiceImpl();
//        int integral=service.queryIntegral(uId);//查询用户积分
//        req.setAttribute("integral",integral);
//        req.getRequestDispatcher("/jsp/hd/Integal.jsp").forward(req,resp);

    }
}
